package com.dd.cloud.user.service.phar.pharservice;

import com.dd.cloud.user.entity.order.OrderPay;
import com.dd.cloud.user.entity.phar.Pharmacy;
import com.dd.cloud.user.entity.pres.PresPrescription;
import com.dd.cloud.common.enums.PharEnum;
import com.dd.cloud.user.service.log.ILogErrorService;
import com.dd.cloud.user.service.phar.IPharmacyService;
import com.dd.cloud.user.service.pres.IPresPrescriptionService;
import com.dd.cloud.common.config.common.SpringContextJobUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 获取药房service类
 * @ClassName: PharServiceBean
 * @Author: czg
 * @Date: 2022/11/26 15:10
 * @Version: 1.0
 */
@Service
public class PharServiceBean implements PharServiceBeanInterface {
    @Autowired
    private IPresPrescriptionService presPrescriptionService;
    @Autowired
    private IPharmacyService pharmacyService;
    @Autowired
    private ILogErrorService logErrorService;
    @Autowired
    private PharDecoratorService pharDecoratorService;

    /**
     * 用订单信息获取药房的service
     *
     * @param pay
     * @return
     */
    @Override
    public PharServiceInterface getPharService(OrderPay pay) {
        if (StringUtils.isBlank(pay.getServerNo())) {
            logErrorService.presBackLog("处方编码为空-数据异常", "获取药房service失败", "pay.orderNo=" + pay.getOrderNo());
            return null;
        }
        return getPharService(pay.getServerNo());
    }

    /**
     * 获取药房service
     *
     * @param pharEnum 药房枚举信息
     * @return
     */
    @Override
    public PharServiceInterface getPharService(PharEnum pharEnum) {
        if (pharEnum == null) {
            return null;
        }
        //pharDecoratorService
        //return SpringContextJobUtil.getBean(pharEnum.getHead() + "PharService", PharServiceInterface.class);
        PharServiceInterface bean = SpringContextJobUtil.getBean(pharEnum.getHead() + "PharService", PharServiceInterface.class);
        pharDecoratorService.setPharService(bean);
        return pharDecoratorService;
    }

    /**
     * 药房id获取药房的service
     *
     * @param pharId 药房id
     * @return
     */
    @Override
    public PharServiceInterface getPharService(Integer pharId) {
        Pharmacy pharmacy = pharmacyService.getById(pharId);
        if (pharmacy == null) {
            logErrorService.presBackLog("药房查询为空-数据异常", "获取药房service失败", "pharId=" + pharId);
            return null;
        }
        PharEnum pharEnum = PharEnum.getByCode(pharmacy.getFlag());
        PharServiceInterface bean = SpringContextJobUtil.getBean(pharEnum.getHead() + "PharService", PharServiceInterface.class);
        pharDecoratorService.setPharService(bean);
        return pharDecoratorService;
    }

    /**
     * 处方编码获取对应药房的service
     *
     * @param tencode 处方编号
     * @return
     */
    @Override
    public PharServiceInterface getPharService(String tencode) {
        PresPrescription presPrescription = presPrescriptionService.getById(tencode);
        if (presPrescription == null) {
            logErrorService.presBackLog("处方查询为空-数据异常", "获取药房service失败", "tencode=" + tencode);
            return null;
        }
        return getPharService(presPrescription.getPharmId());
    }
}
